package com.bw.wxy.mvp.model;

import com.bw.wxy.common.Api;
import com.bw.wxy.factroy.ChangFunction;
import com.bw.wxy.factroy.HttpCode;
import com.bw.wxy.factroy.HttpFactory;
import com.bw.wxy.factroy.HttpType;
import com.bw.wxy.mvp.base.model.BaseModel;
import com.bw.wxy.mvp.contract.LREContract;
import com.bw.wxy.mvp.model.entity.BaseEntity;
import com.bw.wxy.mvp.model.entity.ChannelEntity;
import com.bw.wxy.mvp.model.entity.FollowEntity;
import com.bw.wxy.mvp.model.entity.ImageBannerEntity;
import com.bw.wxy.mvp.model.entity.NewsEntity;
import com.bw.wxy.mvp.model.entity.RecommendEntity;
import com.bw.wxy.mvp.model.entity.TextBannerEntity;
import com.bw.wxy.mvp.model.entity.UserFollowEntity;
import com.bw.wxy.mvp.model.entity.VideoNewsEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class LREModel extends BaseModel implements LREContract.ILREModel {

    @Inject
    public LREModel(){}


    @Override
    public Observable<BaseEntity> requestAll(Map<String, Object>... maps) {
        List<Observable<BaseEntity>> observables=new ArrayList<>();
        for (int i = 0; i < maps.length; i++) {
            Map<String,Object>map=maps[i];
            observables.add(changeToBaseEntity(map));
        }
        return Observable.merge(observables);
    }

    private Observable<BaseEntity> changeToBaseEntity(Map<String,Object>map){
        int code = (int) map.get("code");
        map.remove("code");
        Observable<BaseEntity>ob=null;
        switch (code) {
            case HttpCode.USERCHANNELCODE:
                ob= HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class).requestUserChannels(createBody(map))
                        .map(new ChangFunction<ChannelEntity>());
                break;
            case HttpCode.CHANNELNEWS:
                ob= HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class).requestNews(createBody(map))
                        .map(new ChangFunction<NewsEntity>());
                break;
            case HttpCode.TEXTBANNERCODE:
                ob= HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class).requestTextBanner()
                        .map(new ChangFunction<TextBannerEntity>());
                break;
            case HttpCode.IMAGERBANNERCODE:
                ob= HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class).requestImageBanner()
                        .map(new ChangFunction<ImageBannerEntity>());
                break;
            case HttpCode.RECOMMENDNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class)
                        .requestRecommendNews()
                        .map(new ChangFunction<RecommendEntity>());
                break;
            case HttpCode.FOLLOWNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class)
                        .requestFollowNews(createBody(map))
                        .map(new ChangFunction<FollowEntity>());
                break;
            case HttpCode.USERFOLLOWCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUserFollow(createBody(map))
                        .map(new ChangFunction<UserFollowEntity>());
                break;
            case HttpCode.VIDEONEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENTYPE)
                        .getRetrofit().create(Api.class)
                        .requestVideoNews(createBody(map)).map(
                                new ChangFunction<VideoNewsEntity>());
        }
        return ob;
    }

    @Override
    public Observable<BaseEntity> requestRefreshLoad(Map<String, Object> map) {
        return changeToBaseEntity(map);
    }
}

